package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2023-11-19
 * Time: 12:25
 */
public class DelOperation implements IOPeration {
    int i = 0;
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请写出你要查找的图书书名：");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int location = -1;

        for( i = 0; i < currentSize;i++){
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                location = i;
                break;
            }
        }

        if(i >= currentSize){
            System.out.println("不存在这本书！");
            return;
        }

        for(int j = 0; j < currentSize-1; j++){
            Book book = bookList.getBook(j+1);
            bookList.setBook(book,j);
        }
        //将不使用的内存空间设置为空指针
        bookList.setBook(null,currentSize-1);

        bookList.setUsedSize(currentSize-1);
    }
}
